#include<string>
#include<vector>
#include<iostream>
#include <fstream>
#include "cart.h"
using namespace std;

int MainMenu();
void ReadFile();
void SaveFile();
void ShowCarts();
void ShowRus();
void AddCart();
void DelCart(int);
void CartDetail();
void RusDetail();
void AddRus();
void DelRus(int);

vector<SnackCart> carts;
vector<Restaurant> rus;
int ch=0,quit=0;

int main()
{
	
	//# test data --- //
	Meal m1("AA",3.5,3),m2("BB",2.8,8);
	SnackCart sc1("Zhang 3","BJ CHY 101st."),sc2("Li 4","BJ HD 15th.");
	sc1.meals.push_back(m1);sc1.meals.push_back(m2);
	Restaurant ru1("xyz","7 Day","SH TT 29th","021-12345678","8:00 AM");
	Restaurant ru2("yyz","Fast 8","DL ZC 13rd","099-63215826","14:00 PM");
	ru1.meals.push_back(m1);ru1.meals.push_back(m2);
	carts.push_back(sc1);carts.push_back(sc2);
	rus.push_back(ru1);rus.push_back(ru2);
	//--- test data # //
	
	while(true)
	{
		switch(MainMenu())
		{
			case 1:ReadFile();break;
			case 2:SaveFile();break;
			case 3:ShowCarts();break;
			case 4:ShowRus();break;
			default:quit=1;break;
		}
		if(quit)
		{
			cout<<"Bye & Wish your next coming.\n";
			break;
		}
	}
	system("pause");
	return 0;
}

int MainMenu()
{
	cout<<"\nWelcome to william's Eating System!\n\n";
	cout<<"1.Load information from file.\n";
	cout<<"2.Save information to file.\n";
	cout<<"3.Show Carts information.\n";
	cout<<"4.Show Restaurants information.\n";
	cout<<"0.Exit.\n";
	cout<<"Have your choice: ";cin>>ch;
	cout<<"\n";
	return ch;
}
void ReadFile()
{
	ifstream fin("cart.txt");
	if(!fin.is_open())
	{
		cout<<"File open error."<<endl;
		system("pause");
		return;
	}
	
	carts.clear();rus.clear();
	string tmp,line,host,place,name,phonenumber,time,mname,mprice,mpop;
	
	while(getline(fin,tmp))
	{
		if(tmp=="_CART_")
		{
			getline(fin,place);
			getline(fin,host);
			SnackCart tsc(host,place);

			while(getline(fin,line) && line!="#")
			{
				mname=line;
				getline(fin,mprice);
				getline(fin,mpop);
				
				Meal tmeal(mname,atof(mprice.c_str()),atoi(mpop.c_str()));
				tsc.meals.push_back(tmeal);
			}
			carts.push_back(tsc);
		}
		else if(tmp=="_RESTAURANT_")
		{
			getline(fin,name);
			getline(fin,host);
			getline(fin,phonenumber);
			getline(fin,time);
			Restaurant tru(host,name,"",phonenumber,time);

			while(getline(fin,line) && line!="#")
			{
				mname=line;
				getline(fin,mprice);
				getline(fin,mpop);
				Meal tmeal(mname,atof(mprice.c_str()),atoi(mpop.c_str()));
				tru.meals.push_back(tmeal);
			}
			rus.push_back(tru);
		}
		else
		{
			while(getline(fin,line) && line!="#"){}
		}
	}
}
void SaveFile()
{
	ofstream fout("cart1.txt");
	if(!fout)
	{
		cout<<"File open error."<<endl;
		system("pause");
		return;
	}
	
	for(int i=0;i<rus.size();i++)
	{
		fout<<"_RESTAURANT_"<<endl;
		for(int j=0;j<rus[i].meals.size();j++)
		{
			fout<<rus[i]<<endl;
		}
		fout<<"#"<<endl;
	}
	
	for(int i=0;i<carts.size();i++)
	{
		fout<<"_CART_"<<endl;
		fout<<carts[i].getPlace()<<endl<<carts[i].getHost()<<endl;
		for(int j=0;j<carts[i].meals.size();j++)
		{
			fout<<carts[i].meals[j].getName()<<endl<<carts[i].meals[j].getPrice()<<endl<<carts[i].meals[j].getPop()<<endl;
		}
		fout<<"#"<<endl;
	}
	fout.close();
}
void ShowCarts()
{
	cout<<"Carts information:\n";
	for(int i=0;i<carts.size();i++)
	{
		cout<<"Cart "<<i<<" :"<<carts[i]<<endl;
	}
	while(true)
	{
		cout<<"\nWhat would you like to do ?\n";	
		cout<<"1.Add a cart.\n";
		cout<<"2.Del a cart.\n";
		cout<<"3.Cart details.\n";
		cout<<"0.Back to Main Menu.\n";
		cin>>ch;cout<<"\n";
		switch(ch)
		{
			case 1:AddCart();break;
			case 2:DelCart(-1);break;
			case 3:CartDetail();break;
			default:return;
		}
	}

}
void ShowRus()
{
	cout<<"Show Restaurants information:\n";
	for(int i=0;i<rus.size();i++)
	{
		cout<<"Restaurant "<<i<<" :\n"<<rus[i]<<endl;
	}
	while(true)
	{
		cout<<"\nWhat would you like to do ?\n";	
		cout<<"1.Add a Restaurant.\n";
		cout<<"2.Del a Restaurant.\n";
		cout<<"3.Restaurant details.\n";
		cout<<"0.Back to Main Menu.\n";
		cin>>ch;
		switch(ch)
		{
			case 1:AddRus();break;
			case 2:DelRus(-1);break;
			case 3:RusDetail();break;
			default:return;
		}
	}
}
void AddCart()
{
	string host,place;
	cout<<"Input data like this: host place\n";
	cin>>host>>place;
	
	SnackCart tsc(host,place);
	carts.push_back(tsc);
}
void DelCart(int num)
{
	if(num<0)
	{
		cout<<"Which Cart:";
		cin>>num;
	}
	for(vector<SnackCart>::iterator it=carts.begin();it!=carts.end();it++)
		if(it-carts.begin()==num) carts.erase(it);
}
void AddRus()
{
	string host,name,place,phonenumber,time;
	cout<<"Input data like this: host name place phonenumber time\n";
	cin>>host>>name>>place>>phonenumber>>time;
	Restaurant ru(host,name,place,phonenumber,time);
	rus.push_back(ru);
}
void DelRus(int num)
{
	if(num<0)
	{
		cout<<"Which Restaurant:";
		cin>>num;
	}
	for(vector<Restaurant>::iterator it=rus.begin();it!=rus.end();it++)
		if(it-rus.begin()==num) rus.erase(it);
}
void CartDetail()
{
	cout<<"Which Cart:";
	cin>>ch;
	if(ch>=0&&ch<carts.size())
	{
		cout<<carts[ch];
		int lowest=0;
		for(int i=0;i<carts[ch].meals.size();i++)
		{
			cout<<"\nMeal "<<i<<" :"<<carts[ch].meals[i];
			if(carts[ch].meals[i].getPrice()<=carts[ch].meals[lowest].getPrice())
				lowest=i;
		}
		if(carts[ch].meals.size()>0)
		{
		cout<<"\nThe cheapest meal:"<<carts[ch].meals[lowest]<<endl;
		
		//Modify meal data
		cout<<"Want to modify the meal data ?[y/n]";
		char c;cin>>c;
		if(c=='y' || c=='Y')
		{
			cout<<"Input data like this:Mealnum Price PopularLevel\n";
			int num,price,poplv;
			cin>>num>>price>>poplv;
			if(num<0 || num>=carts[ch].meals.size() || price<0 || price>100 || poplv<0 || poplv>10)
				cout<<"Wrong input!"<<endl;
			else
			{
				carts[ch].meals[num].setPrice(price);
				carts[ch].meals[num].setPop(poplv);
			}
			
			for(int i=0;i<carts[ch].meals.size();i++)
			{
				cout<<"\nMeal "<<i<<" :"<<carts[ch].meals[i];
				if(carts[ch].meals[i].getPrice()<=carts[ch].meals[lowest].getPrice())
				lowest=i;
			}
			cout<<"\nThe cheapest meal:"<<carts[ch].meals[lowest]<<endl<<endl;
		}
		}
	}
	else
	{cout<<"Wrong number!\n";}
}
void RusDetail()
{
	cout<<"Which Restaurant:";
	cin>>ch;
	if(ch>=0&&ch<rus.size())
	{
		cout<<rus[ch];
		int popest=0;
		for(int i=0;i<rus[ch].meals.size();i++)
		{
			cout<<"\nMeal "<<i<<" :"<<rus[ch].meals[i];
			if(rus[ch].meals[i].getPop()>=rus[ch].meals[popest].getPop())
				popest=i;
		}
		if(carts[ch].meals.size()>0)
		{
		cout<<"\nThe most popular meal:"<<rus[ch].meals[popest]<<endl<<endl;
		
		//Modify meal data
		cout<<"Want to modify the meal data ?[y/n]";
		char c;cin>>c;
		if(c=='y' || c=='Y')
		{
			cout<<"Input data like this:'Meal num' 'Price' 'Popular Level\n";
			int num,price,poplv;
			cin>>num>>price>>poplv;
			if(num<0 || num>=rus[ch].meals.size() || price<0 || price>100 || poplv<0 || poplv>10)
				cout<<"Wrong input!"<<endl;
			else
			{
				rus[ch].meals[num].setPrice(price);
				rus[ch].meals[num].setPop(poplv);
			}
			
			for(int i=0;i<rus[ch].meals.size();i++)
			{
				cout<<"\nMeal "<<i<<" :"<<rus[ch].meals[i];
				if(rus[ch].meals[i].getPop()>=rus[ch].meals[popest].getPop())
					popest=i;
			}
			cout<<"\nThe most popular meal:"<<rus[ch].meals[popest]<<endl;
		}
		}
	}
	else
	{cout<<"Wrong number!\n";}
}
